/*
 * Scaffold Application Framework
 *
 * Copyright (c) 2010 Sii Sp. z o.o. All rights reserved.
 */
package com.rstrulak.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.rstrulak.constant.FieldLength;

/**
 * This class is used to represent available user roles.
 * 
 * @author Jakub Ciechan
 * @author Wojciech Kochman
 * @version 1.1
 */
@Entity
@Table(name = "ROLES")
public class Role implements java.io.Serializable {
    /**
     * Id generated for serialization purposes.
     */
    private static final long serialVersionUID = -4603680345454192881L;
    
    public static final String NAME = "name";
    public static final String DESCRIPTION = "description";
    /**
     * Role id.
     */
    private Long id;
    /**
     * Role name.
     */
    private String name;
    /**
     * Role description.
     */
    private String description;
    /**
     * Set of permissions associated with the role.
     */
    private Set<Permission> permissions = new HashSet<Permission>(0);

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Role() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator="role_seq_gen")
    @SequenceGenerator(name="role_seq_gen", sequenceName="ROLE_SEQ")
    @Column(name = "ID", precision = 22, scale = 0)
    public Long getId() {
        return id;
    }

    @Column(name = "NAME", nullable = false, length = FieldLength.SHORT_TEXT)
    public String getName() {
        return this.name;
    }

    @Column(name = "DESCRIPTION", length = FieldLength.LONG_TEXT)
    public String getDescription() {
        return this.description;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "ROLE_PERMISSION", joinColumns = { @JoinColumn(name = "ROLE_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "PERMISSION_ID", nullable = false, updatable = false) })
    public Set<Permission> getPermissions() {
        return this.permissions;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Role == false) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        Role other = (Role) obj;

        return new EqualsBuilder().append(this.id, other.id).isEquals();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.id).toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", this.id).append("name", this.name).toString();
    }

    // @Override
    // @Transient
    // public boolean isValid() {
    // // TODO implement
    // return true;
    // }
}
